package com.yuwj.chinaa.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.activity.AssetsBagDetailActivity;
import com.yuwj.chinaa.activity.AssetsNormalUserIntroduceActivity;
import com.yuwj.chinaa.activity.AssetsPartnerUserIntroduceActivity;
import com.yuwj.chinaa.base.BaseLazyLoadFragment;
import com.yuwj.chinaa.entity.EntityAssetsBag;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentMain extends BaseLazyLoadFragment {

    @BindView(R.id.tv_user_normal)
    TextView tvUserNormal;
    @BindView(R.id.tv_user_partner)
    TextView tvUserPartner;
    @BindView(R.id.rv_china_assets)
    RecyclerView rvChinaAssets;
    List<EntityAssetsBag> entityAssetsBagListNormal;//普通用户资产包集合
    List<EntityAssetsBag> entityAssetsBagListPartner;//合伙人资产包集合
    CommonAdapter<EntityAssetsBag> adapterAssetsNormal;
    CommonAdapter<EntityAssetsBag> adapterAssetsPartner;
    int assetsType = 0;//0-普通用户，1-合伙人
    IPresenter presenter;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.fragment_main;
    }

    public static FragmentMain createInstance() {
        FragmentMain fragmentMain = new FragmentMain();
        Bundle bundle = new Bundle();
        fragmentMain.setArguments(bundle);
        return fragmentMain;
    }

    protected void lazyLoad() {
        initParam();
        initView();
        initOperator();
    }

    public void initParam() {
        presenter = new PresenterImpl(this);
        entityAssetsBagListNormal = new ArrayList<>();
        entityAssetsBagListPartner = new ArrayList<>();

        adapterAssetsNormal = new CommonAdapter<EntityAssetsBag>(context, R.layout.item_assets_bag_normal, entityAssetsBagListNormal) {
            @Override
            protected void convert(ViewHolder holder, EntityAssetsBag entityAssetsBag, int position) {
                holder.setText(R.id.tv_assets_name, entityAssetsBag.getAssetName());
                ImageView ivLevel = holder.getView(R.id.iv_level);
                holder.setText(R.id.tv_price, entityAssetsBag.getAssetPriceDJ() + "元");
                holder.setText(R.id.tv_current_amount, "当前可获得：" + entityAssetsBag.getAssetCoinNumber() + "个");
                holder.setText(R.id.tv_total_amount, "总量：" + entityAssetsBag.getAssetCount());
                holder.setText(R.id.tv_remain_amount, "剩余：" + entityAssetsBag.getAssetSurplus());
                int startCount = new Double(entityAssetsBag.getAssetPriceDJ() / 10000).intValue();
                switch (startCount) {
                    case 1:
                        ivLevel.setImageResource(R.mipmap.ic_assets_bag_one_star);
                        break;
                    case 2:
                        ivLevel.setImageResource(R.mipmap.ic_assets_bag_two_star);
                        break;
                    case 3:
                        ivLevel.setImageResource(R.mipmap.ic_assets_bag_three_star);
                        break;
                }
            }
        };
        adapterAssetsNormal.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                EntityAssetsBag entityAssetsBag = entityAssetsBagListNormal.get(position);
                startActivity(AssetsBagDetailActivity.createIntent(context, entityAssetsBag));
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        adapterAssetsPartner = new CommonAdapter<EntityAssetsBag>(context, R.layout.item_assets_bag_partner, entityAssetsBagListPartner) {
            @Override
            protected void convert(ViewHolder holder, EntityAssetsBag entityAssetsBagUserPartner, int position) {
                holder.setText(R.id.tv_assets_name, entityAssetsBagUserPartner.getAssetName());
                ImageView ivLevel = holder.getView(R.id.iv_level);
                holder.setText(R.id.tv_price, entityAssetsBagUserPartner.getAssetPriceDJ() + " china-A");
                holder.setText(R.id.tv_current_amount, "当前可获得：" + entityAssetsBagUserPartner.getAssetCoinNumber() + "个");
                holder.setText(R.id.tv_total_amount, "总量：" + entityAssetsBagUserPartner.getAssetCount());
                holder.setText(R.id.tv_remain_amount, "剩余：" + entityAssetsBagUserPartner.getAssetSurplus());
                switch (entityAssetsBagUserPartner.getAssetName()) {
                    case "China-A业务合伙人":
                        ivLevel.setImageResource(R.mipmap.ic_assets_bag_one_star);
                        break;
                    case "China-A事业合伙人":
                        ivLevel.setImageResource(R.mipmap.ic_assets_bag_two_star);
                        break;
                    case "China-A董事合伙人":
                        ivLevel.setImageResource(R.mipmap.ic_assets_bag_three_star);
                        break;
                }
            }
        };

        adapterAssetsPartner.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                EntityAssetsBag entityAssetsBag = entityAssetsBagListPartner.get(position);
                startActivity(AssetsBagDetailActivity.createIntent(context, entityAssetsBag));
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

    }

    public void initView() {
        rvChinaAssets.setAdapter(adapterAssetsNormal);
        rvChinaAssets.setLayoutManager(new LinearLayoutManager(context));

    }

    public void initOperator() {
        queryAssetsBagList(0);

    }

    public void queryAssetsBagList(int type) {
        JSONObject param = new JSONObject();
        try {
            param.put("assetType", type + "");//资产包类型：0：普通用户，1：合伙人
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.getAssetsList(param.toString());
    }

    @OnClick({R.id.rl_introduce, R.id.tv_user_normal, R.id.tv_user_partner})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_introduce:
                switch (assetsType) {
                    case 0:
                        startActivity(AssetsNormalUserIntroduceActivity.createIntent(context));
                        break;
                    case 1:
                        startActivity(AssetsPartnerUserIntroduceActivity.createIntent(context));
                        break;
                }
                break;
            case R.id.tv_user_normal:
                tvUserNormal.setTextColor(getResources().getColor(R.color.red));
                tvUserNormal.setBackgroundResource(R.mipmap.ic_user_type_select_bg);
                tvUserPartner.setTextColor(getResources().getColor(R.color.white));
                tvUserPartner.setBackgroundColor(Color.argb(0, 0, 0, 0));
                rvChinaAssets.setAdapter(adapterAssetsNormal);
                assetsType = 0;
                if (entityAssetsBagListNormal.size() == 0) {
                    queryAssetsBagList(assetsType);
                }
                break;
            case R.id.tv_user_partner:
                tvUserPartner.setTextColor(getResources().getColor(R.color.red));
                tvUserPartner.setBackgroundResource(R.mipmap.ic_user_type_select_bg);
                tvUserNormal.setTextColor(getResources().getColor(R.color.white));
                tvUserNormal.setBackgroundColor(Color.argb(0, 0, 0, 0));
                rvChinaAssets.setAdapter(adapterAssetsPartner);
                assetsType = 1;
                if (entityAssetsBagListPartner.size() == 0) {
                    queryAssetsBagList(assetsType);
                }
                break;

        }

    }


    @Override
    public void onGetAssetsListSuccess(List<EntityAssetsBag> assetsBagList) {
        super.onGetAssetsListSuccess(assetsBagList);
        if (assetsBagList != null && assetsBagList.size() > 0) {
            switch (assetsType) {
                case 0:
                    entityAssetsBagListNormal.clear();
                    entityAssetsBagListNormal.addAll(assetsBagList);
                    adapterAssetsNormal.notifyDataSetChanged();
                    break;
                case 1:
                    entityAssetsBagListPartner.clear();
                    entityAssetsBagListPartner.addAll(assetsBagList);
                    adapterAssetsPartner.notifyDataSetChanged();
                    break;
            }
        }
    }

    @Override
    public void onGetAssetsListFail(Exception e) {
        super.onGetAssetsListFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }
}
